﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagChina.Basic.CCT.Db.CCT_Db;
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Windows.Data;

namespace Proxy
{
    public class DeleteInfoHandle
    {
        public DeleteInfoHandle()
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeletedSettingInfo(int Id, string Type)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    if (Type == "Appliances")
                    {
                        var Info = Db.T_MachineInfo.FirstOrDefault(s => s.Id == Id);
                        Db.T_MachineInfo.Remove(Info);
                        result = Db.SaveChanges() > 0;
                    }
                    else if (Type == "DirectionConfig")
                    {
                        var aa = Db.T_DirectionConfig.Where(s => s.Id == Id).FirstOrDefault();
                        Db.T_DirectionConfig.Remove(aa);
                        result = Db.SaveChanges() > 0;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Upi"></param>
        /// <returns></returns>
        public bool DeleteBoardsInfos(List<int> Ids)
        {
            bool result = false;
            try
            {
                using (var Db = new Db_CCHS())
                {
                    var PartInfo = Db.T_CCHS_Boardlibrary.Where(s => Ids.Contains(s.Id)).ToList();
                    Db.T_CCHS_Boardlibrary.RemoveRange(PartInfo);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Upi"></param>
        /// <returns></returns>
        public bool ClearStockInfos(List<int> Ids)
        {
            bool result = false;
            DateTime datetime = DateTime.Now;
            try
            {
                using (var Db = new Db_CCHS())
                {
                    var StockInfo = Db.T_CCHS_Stock.Where(s => Ids.Contains(s.Id)).ToList();
                    foreach (var item in StockInfo)
                    {
                        var StockDetailInfo = Db.T_CCHS_StockDetail.Where(s => s.Positioncode == item.Positioncode).ToList();
                        Db.T_CCHS_StockDetail.RemoveRange(StockDetailInfo);

                        item.StackId = string.Empty;
                        item.Material = string.Empty;
                        item.ActualQuantity = 0;
                        item.VirtualQuantity = 0;
                        item.ActualHight = 0;
                        item.VirtualHight = 0;
                        item.Description = string.Empty;
                        item.CreationDate = null;
                        item.ModificationDate = datetime;
                    }

                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Upi"></param>
        /// <returns></returns>
        public bool DeletJobInfos(List<int> Ids)
        {
            bool result = false;
            try
            {
                using (var Db = new Db_CCHS())
                {
                    var JobInfo = Db.T_CCHS_JobList.Where(s => Ids.Contains(s.Id)).ToList();
                    foreach (var item in JobInfo)
                    {
                        if (item.State == 1)//如果入库和出库任务在开启状态下，正在执行的任务不可以删除
                        {
                            continue;
                        }
                        var TaskInfo = Db.T_CCHS_TaskDetail.Where(s => s.JobId.Equals(item.JobId)
                                                                && s.StackId.Equals(item.StackId));
                        var StackInfo = Db.T_CCHS_StackInfo.Where(s => s.JobId.Equals(item.JobId)
                                                                && s.StackId.Equals(item.StackId));
                        var StackDetail = Db.T_CCHS_StackDetail.Where(s => s.JobId.Equals(item.JobId)
                                                                && s.StackId.Equals(item.StackId));

                        //var StockInfo = Db.T_CCHS_Stock.FirstOrDefault(s => s.StackId.Equals(item.StackId));
                        //StockInfo.StackId = string.Empty;
                        //StockInfo.Material = string.Empty;
                        //StockInfo.ActualQuantity = 0;
                        //StockInfo.VirtualQuantity = 0;

                        var StockInfoDetail = Db.T_CCHS_StockDetail.Where(s => s.StackId.Equals(item.StackId) && s.State != 30);

                        Db.T_CCHS_TaskDetail.RemoveRange(TaskInfo);
                        Db.T_CCHS_StackInfo.RemoveRange(StackInfo);
                        Db.T_CCHS_StackDetail.RemoveRange(StackDetail);
                        Db.T_CCHS_StockDetail.RemoveRange(StockInfoDetail);
                        Db.T_CCHS_JobList.Remove(item);
                    }
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        /// 取消脚本任务
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public bool CancelJob(List<int> Ids)
        {
            bool result = false;
            try
            {
                using (var Db = new Db_CCHS())
                {
                    var PartInfo = Db.T_CCHS_JobList.Where(s => Ids.Contains(s.Id)).ToList();
                    foreach (var item in PartInfo)
                    {
                        if (true && item.State == 0)//如果入库和出库任务在开启状态下，正在执行的任务不可以取消
                        {
                            continue;
                        }
                        item.State = 3;
                    }

                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeleteStockDetail(int Id)
        {
            bool result = false;
            try
            {
                using (Db_CCHS Db = new Db_CCHS())
                {

                    T_CCHS_StockDetail StockDetailInfo = Db.T_CCHS_StockDetail.Where(s => s.Id == Id).FirstOrDefault();
                    Db.T_CCHS_StockDetail.Remove(StockDetailInfo);
                    Db.SaveChanges();
                    //删除后,重新排序Layer
                    List<T_CCHS_StockDetail> StockDetailList = Db.T_CCHS_StockDetail.Where(s => s.Positioncode == StockDetailInfo.Positioncode && s.Material == StockDetailInfo.Material).OrderBy(s => s.Layer).ToList();
                    int layer = 1;
                    foreach (var item in StockDetailList)
                    {
                        item.Layer = layer;
                        layer++;
                    }
                    T_CCHS_Stock stockInfo = Db.T_CCHS_Stock.FirstOrDefault(s => s.Positioncode.Equals(StockDetailInfo.Positioncode) && s.Material.Equals(StockDetailInfo.Material));
                    stockInfo.ActualQuantity = Db.T_CCHS_StockDetail.Where(s => s.Positioncode.Equals(StockDetailInfo.Positioncode) && s.Material.Equals(StockDetailInfo.Material)).Count();
                    return Db.SaveChanges() > 0;

                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }
    }
}